package com.example.lianjuan.ui.adapter;

import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lianjuan.Model.domain.SelectCategories;
import com.example.lianjuan.R;

import java.util.ArrayList;
import java.util.List;

public class selectAdapter extends RecyclerView.Adapter<selectAdapter.InnerHolder> {
    private List<SelectCategories.DataDTO> selectList = new ArrayList<>();
    private int current = 0;
    private onCategoryItemClickListener mCategoryItemClick = null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.select_categor, parent, false);
        return new InnerHolder(inflate);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        //进入界面时给第一个页面设置颜色
        if (current == position) {
            holder.categorTv.setBackgroundColor(holder.itemView.getResources().getColor(R.color.colorEFEEEE, null));
        } else {
            holder.categorTv.setBackgroundColor(holder.itemView.getResources().getColor(R.color.white, null));
        }
        SelectCategories.DataDTO dataDTO = selectList.get(position);
        holder.categorTv.setText(dataDTO.getFavorites_title());
        holder.categorTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCategoryItemClick != null && current != holder.getBindingAdapterPosition()) {
                    mCategoryItemClick.onCategoryItemClick(dataDTO);
                    current = holder.getBindingAdapterPosition();
                    notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return selectList.size();
    }

    public void setSelectData(List<SelectCategories.DataDTO> categoriesContent) {
        selectList.clear();
        selectList.addAll(categoriesContent);
        notifyDataSetChanged();
        if (mCategoryItemClick != null && selectList.size() > 0) {
            mCategoryItemClick.onCategoryItemClick(selectList.get(current));
        }
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        private TextView categorTv;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            categorTv = itemView.findViewById(R.id.categor_text);
        }
    }

    public void setCategoryItemClickListener(onCategoryItemClickListener categoryItemClick) {
        mCategoryItemClick = categoryItemClick;
    }

    ;

    public interface onCategoryItemClickListener {
        void onCategoryItemClick(SelectCategories.DataDTO item);
    }
}
